# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime

requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告

'''
1688--获取首页信息
1.url为搜索商品的请求地址
'''
class Alibaba():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        self.cookie = cookiejar.CookieJar()
        ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        self.headers = {
            # "User-Agent": ua.random,
            'authority': 'data.p4psearch.1688.com',
            'method': 'GET',
            'path': '/data/ajax/get_premium_offer_list.json?beginpage=2&asyncreq=1&keywords=%E5%A4%A7%E6%9D%BF&sortType=&descendOrder=&province=&city=&priceStart=&priceEnd=&dis=&cosite=baidujj_pz&location=re&trackid=%7Btrackid%7D&spm=a2609.11209760.j3f8podl.e5rt432e&keywordid=%7Bkeywordid%7D&pageid=3eef348511F2ug&p4pid=37485334c0c141a3891de289f24b8c13&callback=jsonp_1583392709742_23213&_=1583392709742',
            'scheme': 'https',
            'accept': '*/*',
            'accept-encoding': 'gzip, deflate, br',
            'accept-language': 'zh-CN,zh;q=0.9',
            'cookie': 'last_mid=b2b-819869002; __cn_logon__=true; __cn_logon_id__=blackrose426; __last_loginid__=blackrose426; cna=ZornFiaa7Q8CAXd0TgzQORA+; _csrf_token=1583391970628; cookie1=VAn%2BHMs%2BhO40ERXTmqBFeGVi%2BwGLDDUNMN4BpRe3mQc%3D; cookie2=13427cc3f6314841ee5df889e6b2eff4; cookie17=W8g6%2Fie%2FxKX9; hng=CN%7Czh-CN%7CCNY%7C156; t=f5c6a9a379e3b19c925e58c1bf3438a1; _tb_token_=e833e4e311765; sg=62c; csg=6e07a0f0; lid=blackrose426; unb=819869002; uc4=nk4=0%40A6XheqXNkU0e%2BSq5v5m%2Fm0Yd7BeYsT0%3D&id4=0%40WeudRGIxjARxWsLM%2F8GX8PAtISE%3D; ali_apache_track=c_mid=b2b-819869002|c_lid=blackrose426|c_ms=1; ali_apache_tracktmp=c_w_signed=Y; _nk_=blackrose426; __rn_alert__=false; alicnweb=touch_tb_at%3D1583391971011%7Clastlogonid%3Dblackrose426; _is_show_loginId_change_block_=b2b-819869002_false; _show_force_unbind_div_=b2b-819869002_false; _show_sys_unbind_div_=b2b-819869002_false; _show_user_unbind_div_=b2b-819869002_false; UM_distinctid=170a98558aa210-02cd83be542ffa-4313f6b-144000-170a98558ab5e0; taklid=8bdb9fc5a5be4989b027acd988795f70; l=dBrBhkvrQzj5Lo8kBOfgRclVoi_tiQdb8oVr_EBHtICP9HWkeuiNWZqv6vKDCnGVnsakR3rmR6OpBy8zFyUBmxv9-eN48saondLh.; isg=BKCgDY4h6oN7zVaHbkPENQIQca5yqYRzONOy3Bq4L789FUc_w7vUA59nrf0woTxL; JSESSIONID=4EAAC1BFEC7A95C6C068BF0C51D4B6C0',
            'dnt': '1',
            'referer': 'https://p4psearch.1688.com/p4p114/p4psearch/offer.htm?keywords=%E5%A4%A7%E6%9D%BF&cosite=baidujj_pz&location=re&trackid=%7Btrackid%7D&spm=a2609.11209760.j3f8podl.e5rt432e&keywordid=%7Bkeywordid%7D',
            'sec-fetch-dest': 'script',
            'sec-fetch-mode': 'no-cors',
            'sec-fetch-site': 'same-site',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36'
        }

    def get_contents(self):
        # 搜索商品URL
        url = "https://data.p4psearch.1688.com/data/ajax/get_premium_offer_list.json?beginpage=3&asyncreq=1&keywords=%E5%A4%A7%E6%9D%BF&sortType=&descendOrder=&province=&city=&priceStart=&priceEnd=&dis=&cosite=baidujj_pz&location=re&trackid=%7Btrackid%7D&spm=a2609.11209760.j3f8podl.e5rt432e&keywordid=%7Bkeywordid%7D&pageid=3eef348511F2ug&p4pid=37485334c0c141a3891de289f24b8c13&callback=jsonp_1583394223365_85310&_=1583394223365"
        html = HttpUtils.do_request("GET", url, self.headers, "")
        datajson = json.loads(html.text.replace("jsonp_1583392709742_23213", "").replace("(", "").replace(")", "").replace(";", ""))
        pass


if __name__ == '__main__':
    alibaba = Alibaba()
    alibaba.get_contents()